package com.retail.booking.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.retail.booking.entity.po.BookingXiaohesuanPo;
import com.retail.booking.entity.request.BookingXiaohesuanRequest;
import com.retail.booking.entity.vo.BookingSharePosterConfigVo;
import com.retail.booking.entity.vo.BookingXiaohesuanVo;
import com.retail.booking.mapper.BookingXiaohesuanMapper;
import com.retail.booking.service.BookingSharePosterConfigService;
import com.retail.booking.service.BookingXiaohesuanService;
import com.retail.core.entity.ParamReq;
import com.retail.system.entity.vo.AdminVo;
import com.retail.system.entity.vo.DeptVo;
import com.retail.system.mapper.AdminMapper;
import com.retail.system.mapper.DeptMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 小核酸预约 Service impl
 *
 * @author Administrator
 */
@Service
public class BookingXiaohesuanServiceImpl implements BookingXiaohesuanService {

    @Resource
    private BookingXiaohesuanMapper bookingXiaohesuanMapper;

    @Resource
    private BookingSharePosterConfigService bookingSharePosterConfigService;

    @Resource
    private AdminMapper userMapper;

    @Resource
    private DeptMapper deptMapper;

    @Override
    public BookingXiaohesuanVo getModel(Long id) {

        BookingXiaohesuanVo bookingXiaohesuanPo = bookingXiaohesuanMapper.getModel(id);


        if (ObjectUtil.isNotEmpty(bookingXiaohesuanPo.getPosterId())) {
            BookingSharePosterConfigVo posterConfigVo = getBookingSharePosterConfigById(bookingXiaohesuanPo.getPosterId());
            if (ObjectUtil.isNotEmpty(posterConfigVo)) {
                bookingXiaohesuanPo.setPosterName(posterConfigVo.getPosterName());
            }
        }

        if (ObjectUtil.isNotEmpty(bookingXiaohesuanPo.getUserId())) {
            AdminVo userVo = userMapper.getModel(bookingXiaohesuanPo.getUserId());
            if (ObjectUtil.isNotEmpty(userVo)) {
                bookingXiaohesuanPo.setAvatarUrl(userVo.getUrl());
                bookingXiaohesuanPo.setCustomerId(userVo.getId());
            }
        }


        if (ObjectUtil.isNotEmpty(bookingXiaohesuanPo.getShareUserId())) {
            AdminVo userVo = userMapper.getModel(bookingXiaohesuanPo.getShareUserId());
            if (ObjectUtil.isNotEmpty(userVo)) {
                bookingXiaohesuanPo.setShareUserName(userVo.getName());
            }
        }

        return bookingXiaohesuanPo;
    }

    @Transactional
    @Override
    public Long save(BookingXiaohesuanRequest request) {
        bookingXiaohesuanMapper.save(request);
        return request.getId();
    }

    @Transactional
    @Override
    public Long update(BookingXiaohesuanRequest request) {
        bookingXiaohesuanMapper.update(request);
        return request.getId();
    }


    @Override
    public List<BookingXiaohesuanVo> listPage(ParamReq<BookingXiaohesuanRequest> params) {
        params.setStartIndex((params.getCurrent() - 1) * params.getSize());
        
        List<BookingXiaohesuanVo> res = CollUtil.newArrayList();
        for (BookingXiaohesuanVo bookingXiaohesuanPo : res) {
            
           
            if (ObjectUtil.isNotEmpty(bookingXiaohesuanPo.getPosterId())) {
                BookingSharePosterConfigVo posterConfigVo = getBookingSharePosterConfigById(bookingXiaohesuanPo.getPosterId());
                if (ObjectUtil.isNotEmpty(posterConfigVo)) {
                    bookingXiaohesuanPo.setPosterName(posterConfigVo.getPosterName());
                }
            }
            if (ObjectUtil.isNotEmpty(bookingXiaohesuanPo.getUserId())) {
                AdminVo userVo = userMapper.getModel(bookingXiaohesuanPo.getUserId());
                if (ObjectUtil.isNotEmpty(userVo)) {
                    bookingXiaohesuanPo.setAvatarUrl(userVo.getUrl());
                    bookingXiaohesuanPo.setCustomerId(userVo.getId());
                }
            }

            if (ObjectUtil.isNotEmpty(bookingXiaohesuanPo.getShareUserId())) {
                AdminVo userVo = userMapper.getModel(bookingXiaohesuanPo.getShareUserId());
                if (ObjectUtil.isNotEmpty(userVo)) {
                    bookingXiaohesuanPo.setShareUserName(userVo.getName());
                }
            }
        }
        return res;
    }

    @Override
    public Integer countPage(ParamReq<BookingXiaohesuanRequest> params) {
        return bookingXiaohesuanMapper.countPage(params);
    }

    @Override
    public Integer updateDelFlagById(Long id, Integer delFlag) {
        return bookingXiaohesuanMapper.updateDelFlagById(id, delFlag);
    }

    /**
     * 根据海报编号查询海报信息
     *
     * @param id
     * @return
     */
    private BookingSharePosterConfigVo getBookingSharePosterConfigById(Long id) {
        return bookingSharePosterConfigService.getModel(id);
    }


    @Override
    public BookingXiaohesuanVo findXiaohesuan(String phone, String yuyueTime, Long deptId) {
        BookingXiaohesuanPo bxhp = bookingXiaohesuanMapper.findXiaohesuan(phone, yuyueTime, deptId);
        if (ObjectUtil.isEmpty(bxhp)) {
            return null;
        }
        BookingXiaohesuanVo bxhv = new BookingXiaohesuanVo();
        BeanUtils.copyProperties(bxhp, bxhv);
        DeptVo deptPo = deptMapper.getModel(bxhv.getDeptId());
        if (ObjectUtil.isNotEmpty(deptPo)) {
            bxhv.setYuyueDeptName(deptPo.getDeptName());
        }
        return bxhv;
    }
}